package ZS1;
import java.util.List;
import java.util.Stack;
/**
 * @author xianyu
 * @version 1.0
 * @date 2020/7/24 11:39
 */
public class Main4 {

    public static void main(String[] args){
        Stack<Integer> stack = new Stack<>();

        stack.push(4);
        stack.push(3);
        stack.push(2);
        stack.push(1);

        System.out.println(stack);
        System.out.println(help(stack));
        System.out.println(stack);
//        reverse(stack);
//        System.out.println(stack);

    }


    public static int help(Stack<Integer> stack){
        int res = stack.pop();
        if(stack.isEmpty()){
            return  res;
        }else{
            int last = help(stack);
            stack.push(res);
            return last;
        }
    }



    public static void reverseStack(Stack<Integer> stack){
        if(stack.isEmpty()) return;
        int i = help(stack);
        reverseStack(stack);
        stack.push(i);
    }




    // 逆序栈
    public static void reverse(Stack<Integer> stack){
        if(stack.isEmpty()){
            return;
        }
        int i = getAndRemoveLastElement(stack);
        reverse(stack);
        stack.push(i);
    }

    // 删除并返回栈底元素
    public static int getAndRemoveLastElement(Stack<Integer> stack){
        int res = stack.pop();
        if(stack.isEmpty()){
            return res;
        }else{
            int last = getAndRemoveLastElement(stack);
            stack.push(res);
            return last;
        }
    }



}
